using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Engine.FileFormats3D.WR2TrafficFiles
{
    public class SpeedBlock
    {
        #region Private Fields

        private int index;
        private short shapeID;
        private byte speed;
        private byte speed2;
        private byte start3;
        private byte start4;
        private byte start5;
        private byte start6;
        private List<int> usedBySplineNo;

        #endregion Private Fields

        #region Public Constructors

        public SpeedBlock(short shapeID, byte Speed)
        {
            this.shapeID = shapeID;
            this.speed2 = 0xAF;
            this.speed = Speed;
            this.start3 = 0;
            this.start4 = 0;
            this.start5 = 0;
            this.start6 = 0;
        }

        public SpeedBlock(BinaryReader br, int index)
        {
            this.index = index;

            this.shapeID = br.ReadInt16();
            this.speed2 = br.ReadByte();
            this.speed = br.ReadByte();
            this.start3 = br.ReadByte();
            this.start4 = br.ReadByte();
            this.start5 = br.ReadByte();
            this.start6 = br.ReadByte();
            usedBySplineNo = new List<int>();
        }

        #endregion Public Constructors

        #region Public Properties

        public short ShapeID
        {
            get { return shapeID; }
            set { shapeID = value; }
        }

        public byte Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public void AddUsedBySpline(int splineID)
        {
            this.usedBySplineNo.Add(splineID);
        }

        public string ListUsedSplines()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("\n\nSpeedblock No: " + this.index.ToString());
            for (int i = 0; i < this.usedBySplineNo.Count; i++)
            {
                sb.AppendLine(this.usedBySplineNo[i].ToString());
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("-SpeedBlock- " + index.ToString() + " ----------");
            sb.AppendLine("ShapeID     " + ShapeID.ToString());
            sb.AppendLine("start1:     " + speed2.ToString());
            sb.AppendLine("speed:     " + Speed.ToString());
            sb.AppendLine("start3:     " + start3.ToString());
            sb.AppendLine("start4:     " + start4.ToString());
            sb.AppendLine("start5:     " + start5.ToString());
            sb.AppendLine("start6:     " + start6.ToString());
            sb.AppendLine("-end-");

            return sb.ToString();
        }

        #endregion Public Methods

        #region Internal Methods

        internal void Save(BinaryWriter bw)
        {
            //bw.Write(index);
            bw.Write(ShapeID);
            bw.Write((byte)(Speed + 10));
            bw.Write(Speed);
            bw.Write(start3);
            bw.Write(start4);
            bw.Write((byte)56);//start5);
            bw.Write(start6);
        }

        #endregion Internal Methods
    }
}